<template>
  <el-container>
    <el-header>
      <div class="hleft">
        <img src="../assets/logo.png" />
        <h3>电商后台管理系统</h3>
      </div>
      <el-button type="danger" @click="quit">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="iswidth">
        <div class="unfoldbtn" @click="unlist">||||</div>
          <el-menu active-text-color="blue"
          background-color="rgb(49, 55, 67)"
          text-color="white"
          unique-opened
          :collapse="islist"
          :collapse-transition="false"
          :router="true"
          :default-active="rtstr"
          ref="menuclass"
          >
      <el-submenu :index="String(item.id)" v-for="item in menuslist" :key="item.id">
        <template slot="title">
          <i class="el-icon-location"></i>
          <span>{{item.authName}}</span>
        </template>
        <el-menu-item-group v-for="nitem in item.children" :key="nitem.id">
          <el-menu-item :index="'/'+nitem.path"  @click="saverouter('/'+nitem.path)">
             <i class="el-icon-menu"></i>
            {{nitem.authName}}
            </el-menu-item>
        </el-menu-item-group>
      </el-submenu>
    </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuslist:[],
      islist:false,
      rtstr:'',
    }
  },
  created(){
    this.getlist()
    this.rtstr = window.sessionStorage.getItem('rtstr')
  },
  computed:{
    iswidth(){
      return this.islist ? "64px" : "200px"
    }
  },
  methods: {
    quit() {
      window.sessionStorage.removeItem("token");
      window.sessionStorage.removeItem("rtstr");
      this.$router.replace("/login");
    },
    async getlist(){
       const {data:res} = await this.$http.get('menus')
      if(res.meta.status!==200) return this.$message.error(res.meta.msg)
      this.menuslist = res.data
    },
    unlist(){
      this.islist = !this.islist
    },
    saverouter(routerstr){
      window.sessionStorage.setItem('rtstr',routerstr)
      this.rtstr = window.sessionStorage.getItem('rtstr')

    }
  },
};
</script>

<style scoped>
.el-container {
  height: 100%;
}
.hleft {
  width: 220px;
  height: 60px;
  display: flex;
  justify-content: space-between;
  color: white;
}
.hleft img {
  float: left;
  width: 60px;
  height: 60px;
}
.el-button {
  margin-top: 10px;
  height: 40px;
}
.el-header {
  display: flex;
  justify-content: space-between;
  background-color: rgb(54, 61, 64);
}
.el-aside {
  background-color: rgb(49, 55, 67);
}
.el-menu{
  border: 0;
}
.unfoldbtn{
  height: 20px;
  text-align: center;
  line-height: 20px;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.el-main {
  background-color: rgb(233, 237, 240);
}
</style>